package com.qdxwx.data.service.course;

import com.qdxwx.common.page.PageRequest;
import com.qdxwx.common.utils.ObjectUtils;
import com.qdxwx.data.mapper.course.CourseAlbumItemMapper;
import com.qdxwx.data.mapper.course.CourseMapper;
import com.qdxwx.data.mapper.course.CoursePackageMapper;
import com.qdxwx.data.service.member.MemberRecordService;
import com.qdxwx.data.service.member.MemberTypeService;
import com.qdxwx.models.course.Course;
import com.qdxwx.models.course.CourseDTO;
import com.qdxwx.models.course.CourseNode;
import com.qdxwx.models.course.CourseQuery;
import com.qdxwx.models.member.MemberRecord;
import com.qdxwx.models.member.MemberType;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Course接口
 * Qdx NetSchool Platform
 * @author qdxwx
 * @updateTime 2022/5/25
 * @description
 */
@Service
public class CourseServiceImpl implements CourseService {
	@Autowired
	private CourseMapper courseMapper;
	@Autowired
	private CoursePackageMapper coursePackageMapper;
	@Autowired
	private CourseAlbumItemMapper courseAlbumItemMapper;
	@Autowired
	private CourseNodeService courseNodeService;
	@Autowired
	private MemberRecordService memberRecordService;
	@Autowired
	private MemberTypeService memberTypeService;

	@Override
	public int insert(Course course) {
		course.setUpdateTime(LocalDateTime.now());
		course.setAddTime(LocalDateTime.now());
		return courseMapper.insertCourse(course);
	}

	@Override
	public int delete(int id) {
		//删除该课程的相关套餐
		coursePackageMapper.deleteCoursePackage(id,0);
		//删除该课程的相关课程推荐
		courseAlbumItemMapper.deleteCourseAlbumItemByCourseId(id);
		//删除所有章节
		courseNodeService.deleteByCourseId(id);
		return courseMapper.deleteCourseById(id);
	}

	@Override
	public int update(Course course) {
		course.setUpdateTime(LocalDateTime.now());
		return courseMapper.updateCourse(course);
	}

	@Override
	public void updateView(int courseId) {
		if (courseId<1) return;
		courseMapper.updateViewCount(courseId);
	}

	@Override
	public void updateBuy(int courseId) {
		if (courseId<1) return;
		courseMapper.updateBuyCount(courseId);
	}

	@Override
	public Course get(int courseId) {
		if (courseId < 1) return null;
		return courseMapper.selectById(courseId);
	}

	@Override
	public CourseDTO getDTO(int courseId) {
		if (courseId < 1) return null;
		return courseMapper.selectDTOById(courseId);
	}

	@Override
	public CourseDTO getMyCourse(long userId, int courseId) {
		if (courseId < 1 || userId < 1) return null;
		return courseMapper.selectMyCourse(userId, courseId);
	}

	@Override
	public List<Course> getList(CourseQuery course, PageRequest page) {
		return courseMapper.selectCourseList(course, page);
	}

	@Override
	public int count(CourseQuery course) {
		return courseMapper.count(course);
	}

	@Override
	public List<CourseDTO> getMyCourseList(CourseQuery courseQuery, PageRequest page) {
		return courseMapper.selectMyCourseList(courseQuery, page);
	}

	@Override
	public int countMyCourse(CourseQuery query) {
		return courseMapper.countMyCourse(query);
	}

	@Override
	public List<CourseDTO> getInterfixList(Course course, int limit) {
		return courseMapper.selectInterfixList(course, limit);
	}

	@Override
	public List<CourseDTO> getDTOListByAlbumId(Integer albumId) {
		return courseMapper.selectDTOListByAlbumId(albumId);
	}

	/**
	 * 获得项目专业限制的所有课程
	 */
	@Override
	public List<Course> getCouponSubjectCourse(Integer categoryId, String courseIds) {
		return courseMapper.getCouponSubjectCourse(categoryId, courseIds);
	}


	/**
	 * 后台根据条件获取套餐下课程详细信息
	 */
	@Override
	public List<CourseDTO> getPackageList(Course course, PageRequest page) {
		if (course.getCourseId() < 1) {
			return new ArrayList<>();
		}
		return courseMapper.selectPackageList(course, page);
	}

	@Override
	public int getPackageCount(Course course) {
		if (course.getCourseId() < 1) {
			return 0;
		}
		return courseMapper.selectPackageCount(course);
	}

	@Override
	public List<CourseDTO> getPackageParents(int courseId) {
		if (courseId < 1) return null;
		return courseMapper.selectPackageParents(courseId);
	}

	/**
	 * 获取课程套餐的详细课程列表
	 *
	 * @param ids 查询条件
	 */
	@Override
	public List<CourseDTO> getListByPackageIds(List<Integer> ids) {
		return courseMapper.selectListByPackage(ids);
	}

	@Override
	public List<CourseDTO> getListByPackageId(int id) {
		return null;
	}


	@Override
	public List<Course> getListByOrderNo(String orderNo) {
		return courseMapper.selectListByOrderNo(orderNo);
	}

	/**
	 * @param userId 用户Id
	 * @param type   1课程 2套餐（备用）
	 * @return 查询购物车课程
	 */
	@Override
	public List<Course> getListByShopCart(Long userId, Integer type) {
		return courseMapper.selectListByShopCart(userId, type);
	}

	@Override
	public void setNearTime(List<CourseDTO> courseDTOS) {
		for (CourseDTO courseDto : courseDTOS) {
			/*取直播章节开始结束时间*/
			List<CourseNode> courseNodeList = courseNodeService.getList(courseDto.getCourseId());
			/*把结束时间大于现在时间的直播时间放到开始和结束时间的list中*/
			List<CourseNode> nearestList = courseNodeList.stream().filter(a -> a.getLiveEndTime() != null && LocalDateTime.now().isBefore(a.getLiveEndTime())).collect(Collectors.toList());
			if (nearestList.size() > 0) {
				nearestList.sort(Comparator.comparing(CourseNode::getLiveEndTime));
				courseDto.setNearestLiveEndTime(nearestList.get(0).getLiveEndTime());

				nearestList.sort((o1, o2) -> o2.getLiveBeginTime().compareTo(o1.getLiveBeginTime()));
				courseDto.setNearestLiveBeginTime(nearestList.get(0).getLiveBeginTime());
			}
		}
	}

	@Override
	public boolean hasMember(Integer courseId, Long userId) {
		/*查询课程对应的会员类型*/
		List<MemberType> memberTypes = memberTypeService.getListByCourseId(courseId);
		/*如果用户开通了会员并且课程是会员课程*/
		if (ObjectUtils.isNotNull(memberTypes)) {
			/*判断用户是否开通了课程对应的会员类型*/
			for (MemberType memberType : memberTypes) {
				/*如果用户开通的会员包括课程对应的某个会员类型*/
				MemberRecord memberRecord = memberRecordService.getValidByUserId(userId, memberType.getId());
				if (memberRecord != null) {
					return true;
				}
			}
		}
		return false;
	}

	@Override
	public int updateTotalDurations(int courseId, int totals) {
		if (courseId < 1 || totals < 1) return 0;
		return courseMapper.updateTotalDurations(courseId, totals);
	}
}